//======================================================================================
//======================================================================================
//io
//bk
//sprite
//sound
//sys
//res
//jpg
//joy
//thread
//======================================================================================
//======================================================================================


//======================================================================================
//	INCLUDE
//======================================================================================
#include "G2.h"



//======================================================================================
//	FUNCTION
//======================================================================================
void g2CreateThread(PCThread pThread,
					LPCSTR	lpName,
					PTHREAD_START_ROUTINE	pfnStartAddr,
					int		nPriority,
					DWORD	dwFrame	)
{
	if(pThread->m_bState)
	{
		G2_Printf("Thread have create!");
		return;
	}
	
	SECURITY_ATTRIBUTES	MutexTemp;
	SECURITY_ATTRIBUTES	ThreadTemp;
	DWORD				Temp;
	WCHAR				cTemp[30];
	
	pThread->m_bState=TRUE;
	pThread->m_dwNewTime = GetTickCount();
	pThread->m_dwOldTime  = pThread->m_dwNewTime;
	if(!dwFrame)
		dwFrame=1;
	pThread->m_dwRunTime=dwFrame;
	
	GetUnicodeString((LPTSTR)cTemp, lpName, lage_En);
	pThread->m_hMutex= CreateMutex(&MutexTemp, FALSE,(LPCTSTR)cTemp);
	pThread->m_hThread = CreateThread(&ThreadTemp, 0, pfnStartAddr, NULL, 0, &Temp);
	g2SetThreadPriority(pThread, nPriority);
}

void g2DestroyThread(PCThread pThread)
{
	if(!pThread->m_bState)
	{
		G2_Printf("Thread have destroy!");
		return;
	}
	
	//wait thread exit
	pThread->m_bState=FALSE;
	Sleep(1);
	while(!pThread->m_bState)
	{
		Sleep(1);
	}

	//thread have exited
	WaitForSingleObject(pThread->m_hThread, INFINITE);
	CloseHandle( pThread->m_hThread );
	ReleaseMutex(pThread->m_hMutex);
	pThread->m_hThread = NULL;
	pThread->m_hMutex = NULL;
	pThread->m_bState=FALSE;
	G2_Printf("Destroy Thread Success!");
}

void g2SetThreadFrame(PCThread pThread, DWORD dwFrame)
{
	if((dwFrame>=5)&&(dwFrame<=25))
	{
		pThread->m_dwRunTime=dwFrame;
	}
	else
	{
		pThread->m_dwRunTime=15;
	}
}

void g2CWaitForSingleObject(PCThread pThread)
{
	if(!pThread->m_bState)
		return ;
		
	DWORD dwResult = WaitForSingleObject(pThread->m_hMutex, INFINITE);
	if(WAIT_OBJECT_0 != dwResult )
	{
		G2_Printf("WaitForSingleObject error [%d]", dwResult)
	}
}

void g2CReleaseMutex(PCThread pThread)
{
	if(!pThread->m_bState)
		return ;

	if(! ReleaseMutex(pThread->m_hMutex))
	{
		G2_Printf("ReleaseMutex error ") ;
	}
}

void g2ThreadRun(PCThread pThread)
{
	while(pThread->m_bState)
	{
		pThread->m_dwNewTime = GetTickCount();
		if((pThread->m_dwNewTime-pThread->m_dwOldTime)<(1000/pThread->m_dwRunTime))
		{	
			Sleep( 1+((1000/pThread->m_dwRunTime)-(pThread->m_dwNewTime-pThread->m_dwOldTime))/10 ) ;
		}
		else
		{
			break ;
		}
	}
	pThread->m_dwOldTime  = pThread->m_dwNewTime ;
}

void g2SetThreadPriority(PCThread pThread, int nPriority)
{
	SetThreadPriority(pThread->m_hThread, nPriority);
}

DWORD g2SuspendThread(PCThread pThread)
{
	return SuspendThread(pThread->m_hThread);
}

DWORD g2ResumeThread(PCThread pThread)
{
	return ResumeThread(pThread->m_hThread);
}

void g2SetThreadState(PCThread pThread, BOOL bState)
{
	pThread->m_bState=bState;
}

BOOL g2GetThreadState(PCThread pThread)
{
	return pThread->m_bState;
}


